package cn.com.bsoft.mobile.ui


import cn.com.bsoft.mobile.service.MobileAppService
import cn.net.xyan.blossom.core.service.ConfigService
import cn.net.xyan.blossom.kotlin.StatusAndMessageError
import com.github.vok.karibudsl.*
import com.vaadin.annotations.Theme
import com.vaadin.annotations.Viewport
import com.vaadin.server.Page
import com.vaadin.server.VaadinRequest
import com.vaadin.spring.annotation.SpringUI
import com.vaadin.ui.UI
import com.vaadin.ui.themes.ValoTheme
import org.springframework.beans.factory.annotation.Autowired

@SpringUI(path = "/app_suite")
@Theme("blossom")
@Viewport("width=device-width, initial-scale=1.0")
class AppSuiteUI() : UI() {

    @Autowired
    lateinit var appService:MobileAppService

    @Autowired
    lateinit var configService: ConfigService

    override fun init(request: VaadinRequest?) {
        val suiteID = request?.getParameter("suiteID")?:throw StatusAndMessageError("没有设置参数suiteID")

        val suite = appService.querySuite(suiteID)?:throw StatusAndMessageError("没有找到应用程序套件$suiteID")

        val appRoot = configService.queryValue(ConfigService.KEYAppRoot)

        val content = verticalLayout {
            label("${suite.title}") {
                w = fillParent; styleName = ValoTheme.LABEL_HUGE
                expandRatio = 0.0f
            }

            verticalLayout {
                suite.apps.forEach {
                    app ->
                    button (app.title){
                        onLeftClick {
                            Page.getCurrent().open("$appRoot/app/inHouse/${app.nativeID}", "_self",false)
                        }
                    }

                }
            }
        }

        this.content = content
    }

}